package com.primaryedu.ui.center;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.primaryedu.R;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;
import com.primaryedu.encrpy.EncrpyUtils;
import com.primaryedu.ui.BaseActivity;

/**
 * 修改密码
 *
 * @author jun.wang
 * @description
 * @data 2016年12月15日
 */
public class PasswordModActivity extends BaseActivity implements OnClickListener {
    private EditText edit_oldpassword, edit_newpassword, edit_newpassword_again;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_password_mod);

    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_password_mod;
    }

    @Override
    protected void initView() {
        ((TextView) findViewById(R.id.title)).setText("修改密码");
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.next).setOnClickListener(this);
        edit_oldpassword = (EditText) this.findViewById(R.id.edit_oldpassword);
        edit_newpassword = (EditText) this.findViewById(R.id.edit_newpassword);
        edit_newpassword_again = (EditText) this.findViewById(R.id.edit_newpassword_again);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.next:
                requestData();
                break;

            default:
                break;
        }
    }

    private void requestData() {
        if (TextUtils.isEmpty(edit_oldpassword.getText().toString())) {
            showToast("请输入您的旧密码");
            return;
        }

        if (TextUtils.isEmpty(edit_newpassword.getText().toString())) {
            showToast("请输入您的新密码");
            return;
        }

        if (TextUtils.isEmpty(edit_newpassword_again.getText().toString())) {
            showToast("请输入确认密码");
            return;
        }

        if (!edit_newpassword.getText().toString().equals(edit_newpassword_again.getText().toString())) {
            showToast("两次密码输入不一致");
            return;
        }

        showDialog();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.uploadPassword,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        closeDialog();

                        try {
                            JSONObject json = new JSONObject(response);
                            String reason = json.getString("reason");
                            int result = json.getInt("result");

                            if (result == 1) {
                                showToast(reason);
                                finish();
                            } else {
                                showToast(reason);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                closeDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("userid", GlobalData.getUserId(getApplicationContext()));
                map.put("oldpassword", EncrpyUtils.getPhone(edit_oldpassword.getText().toString()));
                map.put("newpassword", EncrpyUtils.getPhone(edit_newpassword_again.getText().toString()));
                return map;
            }
        };
        requestQueue.add(stringRequest);

    }

}
